package com.lbd.dynamicCode.controller;

import com.lbd.base.controller.BaseController;
import com.lbd.commons.Result;
import com.lbd.commons.ResultState;
import com.lbd.dynamicCode.bean.MsgSendRecord;
import com.lbd.dynamicCode.service.DynamicCodeService;
import com.lbd.mq.MessageSender;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by zzl on 2017/11/19.
 */
@RestController
@RequestMapping("/dynamicCode")
public class DynamicCodeController extends BaseController {
    @Autowired
    private DynamicCodeService dynamicCodeService;
    @Resource
    private MessageSender messageSender;
    /**
     * 按条件分页查询活码短信信息
     *
     * @param page  第几页
     * @param rows  一页多少条
     * @param record
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result list(@RequestParam(name = "page", required = false, defaultValue = "1") int page,
                       @RequestParam(name = "rows", required = false, defaultValue = "10") int rows,MsgSendRecord record) {
        return new Result(ResultState.SUCCESS, "查询成功",dynamicCodeService.findByPageConditionJpql(page, rows, record));
    }

    /**
     * 保存短信信息
     *
     * @param record
     * @return
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Result save(MsgSendRecord record, HttpSession session) {
        dynamicCodeService.save(record);
        return new Result(ResultState.SUCCESS, "添加成功", "");
    }

    /**
     * 根据id查询回显要编辑的Batch
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public Result toEdit(String id) {
        return new Result(ResultState.SUCCESS, "加载成功", dynamicCodeService.findById(id));
    }

    /**
     * 修改短信信息
     *
     * @param record
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public Result update(MsgSendRecord record) {
        dynamicCodeService.update(record);
        return new Result(ResultState.SUCCESS, "更新成功");
    }

    /**
     * 删除批次信息
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/del", method = RequestMethod.POST)
    public Result del(@RequestParam("ids[]") String[] ids) {
        dynamicCodeService.del(ids);
        return new Result(ResultState.SUCCESS, "删除成功", "");

    }

    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public Result upload(@RequestParam("file") MultipartFile file,@RequestParam("batchId")String batchId,HttpServletRequest request){

        ImportParams params = new ImportParams();
        params.setNeedSave(true);
        List<MsgSendRecord> list=null;

        try {
            list= ExcelImportUtil.importExcel(file.getInputStream(), MsgSendRecord.class, params);
        } catch (Exception e) {
            e.printStackTrace();
        }
        for (MsgSendRecord record : list) {
            record.setShortLink("shortlink");
            record.setState(0);//未读
            record.setLandUrl("落地页URL");
            record.setLocatedUrl("localUrl");
            record.setBatchId(batchId);
            messageSender.send(record);
        }
        try {
            System.out.println("业务处理");
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return new Result(ResultState.SUCCESS, "上传成功");
    }

}
